package com.baske.sort;

public class QuickSort {

    public static void main(String[] args) {
        int[] arr = { 1, 4 , 2, 6, 7 };
        quickSort(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] +",");
        }
    }


    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        if(startIndex >= endIndex) {
            return;
        };
        int left = startIndex;
        int right = endIndex;
        int pivot = arr[left];

        while (left < right) {
            while (left < right && arr[right] >= pivot) {
                right--;
            }
            if (left < right) {
                arr[left] = arr[right];
            }
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            if (left < right) {
                arr[right] = arr[left];
            }
            if (left >= right) {
                arr[left] = pivot;
            }
        }
        quickSort(arr,startIndex,right - 1);
        quickSort(arr,right + 1, endIndex);
    }
}
